// var r;
// var AL_GAME_HUA_i18nMgr = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_i18nMgr");
// var AL_GAME_HUA_AddPrefab = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_AddPrefab");
// var AL_GAME_HUA_Common = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Common");
// var AL_GAME_HUA_Define = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Define");
// var AL_GAME_HUA_PopupSelf = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_PopupSelf");
// var AL_GAME_HUA_GameClubButton = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_GameClubButton");
// var AL_GAME_HUA_PlatformManager = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_PlatformManager");
// var AL_GAME_HUA_EventConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_EventConfig");
// var AL_GAME_HUA_WelfareConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_WelfareConfig");
// var AL_GAME_HUA_WelfareData = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_WelfareData");
// var v = cc._decorator;
// var m = v.ccclass;
// var b = v.property;
// var D = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.layout = null;
//         t.com_game_club_button = null;
//         t.inviteRed = null;
//         t.shareRed = null;
//         t.subscribeRed = null;
//         t.gameClubRed = null;
//         t.inviteCompleted = null;
//         t.shareCompleted = null;
//         t.subscribeCompleted = null;
//         t.gameClubCompleted = null;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.start = function () {
//         AL_GAME_HUA_WelfareData.WelfareData.getInstance().chaxunYaoqingShuju();
//         this.chushihua_ui();
//     };
//     t.prototype.onDestroy = function () {
//         AL_APP_SDK_MANAGER.off(this);
//     };
//     t.prototype.chuli_xianshi_hou = function () {
//         this.com_game_club_button.node.active = !0;
//     };
//     t.prototype.chushihua_ui = function () {
//         var e = this.layout.children[3];
//         this.subscribeRed = e.getChildByName("red");
//         this.chushihua_yaoqing();
//         this.chushihua_fenxiang();
//         this.chushihua_yixi_quan_gongneng();
//         this.chushihua_dingyue_gongneng();
//     };
//     t.prototype.chushihua_yaoqing = function () {
//         var e = this;
//         var t = this.layout.children[0];
//         this.inviteRed = t.getChildByName("red");
//         var n = cc.find("rewardBar", t);
//         this.inviteCompleted = cc.find("completed", n);
//         var o = cc.find("topLabel", n).getComponent(cc.Label);
//         var r = AL_GAME_HUA_WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(0).rewardType;
//         var a = AL_GAME_HUA_WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(1).rewardType;
//         var c = AL_GAME_HUA_WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(2).rewardType;
//         var i = r[0].num + a[0].num + c[0].num;
//         o.string = "x" + i.toString();
//         var s = function () {
//             var t = AL_GAME_HUA_WelfareData.WelfareData.getInstance().huoqu_shifou_yongyou_yaoqing_jiangli();
//             e.inviteRed.active = t;
//             var n = AL_GAME_HUA_WelfareData.WelfareData.getInstance().huoqu_suoyou_yaoqing_jiangli();
//             e.inviteCompleted.active = n;
//         };
//         s();
//         AL_APP_SDK_MANAGER.on(
//             AL_GAME_HUA_Define.GameEvent.REFRESH_INVITE_LIST,
//             function () {
//                 s();
//             },
//             this
//         );
//     };
//     t.prototype.chushihua_fenxiang = function () {
//         var e = this;
//         var t = this.layout.children[1];
//         this.shareRed = t.getChildByName("red");
//         var n = cc.find("rewardBar", t);
//         this.shareCompleted = cc.find("completed", n);
//         cc.find("icon", n).getComponent(cc.Sprite);
//         var o = cc.find("topLabel", n).getComponent(cc.Label);
//         var r = AL_GAME_HUA_WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(3).rewardType;
//         o.string = "x" + r[0].num.toString();
//         var a = function () {
//             var t = AL_GAME_HUA_WelfareData.WelfareData.getInstance().huoqu_shifou_yongyou_fenxiang_jiangli();
//             e.shareRed.active = t;
//             e.shareCompleted.active = !t;
//         };
//         a();
//         AL_APP_SDK_MANAGER.on(
//             AL_GAME_HUA_Define.GameEvent.ON_DAILY_SHARE_REFRESH,
//             function () {
//                 a();
//             },
//             this
//         );
//     };
//     t.prototype.chushihua_yixi_quan_gongneng = function () {
//         var e = this;
//         var t = AL_GAME_HUA_WelfareData.WelfareData.getInstance().huoqu_youxiquan_jieshou_jishu();
//         var n = this.layout.children[2];
//         this.gameClubRed = n.getChildByName("red");
//         var o = cc.find("rewardBar", n);
//         this.gameClubCompleted = cc.find("completed", o);
//         cc.find("icon", o).getComponent(cc.Sprite);
//         var r = cc.find("topLabel", o).getComponent(cc.Label);
//         var a = AL_GAME_HUA_WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(0 == t ? 6 : 4).rewardType;
//         r.string = "x" + a[0].num.toString();
//         var c = cc.find("title", n).getComponent(cc.Label);
//         var f = cc.find("desc", n).getComponent(cc.Label);
//         c.string = AL_GAME_HUA_i18nMgr.i18nMgr._huoqu_biaoqian(0 == t ? "加入游戏圈" : "游戏圈互动");
//         f.string = AL_GAME_HUA_i18nMgr.i18nMgr._huoqu_biaoqian(0 == t ? "加入游戏圈描述" : "游戏圈互动描述");
//         var u = function () {
//             var t = AL_GAME_HUA_WelfareData.WelfareData.getInstance().huoqu_shifou_yongyou_youxiquan_jiangli();
//             e.gameClubRed.active = t;
//             e.gameClubCompleted.active = !t;
//         };
//         u();
//         AL_APP_SDK_MANAGER.on(
//             AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_REFRESH,
//             function () {
//                 u();
//             },
//             this
//         );
//         this.com_game_club_button.tianjia_anniu_dianji_huidiao(function () {
//             if (AL_GAME_HUA_WelfareData.WelfareData.getInstance().huoqu_shifou_yongyou_youxiquan_jiangli()) {
//                 AL_GAME_HUA_WelfareData.WelfareData.getInstance().jieshou_youxiquan_jiangli();
//                 AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_jieshou_mianban({
//                     rewards: a
//                 });
//                 AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportOutput({
//                     source: AL_GAME_HUA_EventConfig.AssetsSource.welfare_club,
//                     action: AL_GAME_HUA_EventConfig.AssetsAction.get,
//                     goodsList: a.map(function (e) {
//                         return {
//                             goods: e.goodsId,
//                             num: e.num
//                         };
//                     })
//                 });
//             }
//         });
//     };
//     t.prototype.chushihua_dingyue_gongneng = function () {
//         var e = this;
//         var t = this.layout.children[3];
//         this.subscribeRed = t.getChildByName("red");
//         var n = cc.find("rewardBar", t);
//         this.subscribeCompleted = cc.find("completed", n);
//         cc.find("icon", n).getComponent(cc.Sprite);
//         var o = cc.find("topLabel", n).getComponent(cc.Label);
//         var r = AL_GAME_HUA_WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(5).rewardType;
//         o.string = "x" + r[0].num.toString();
//         var a = function () {
//             var t = AL_GAME_HUA_WelfareData.WelfareData.getInstance().get_Have_Subscribe_Reward();
//             e.subscribeRed.active = t;
//             e.subscribeCompleted.active = !t;
//         };
//         a();
//         AL_APP_SDK_MANAGER.on(
//             AL_GAME_HUA_Define.GameEvent.ON_SUBSCRIBE_REFRESH,
//             function () {
//                 a();
//             },
//             this
//         );
//     };
//     t.prototype.handle_button_callback = function (e, t) {
//         switch (t) {
//             case "close":
//                 this.close();
//                 break;
//             case "invite":
//                 this.close();
//                 AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_yaoqing_mianban();
//                 break;
//             case "share":
//                 AL_GAME_HUA_Common.Common.share({
//                     location: AL_GAME_HUA_Define.Define.SHARE_LOCATION.LOGIN,
//                     type: 3,
//                     success: function () {
//                         if (AL_GAME_HUA_WelfareData.WelfareData.getInstance().huoqu_shifou_yongyou_fenxiang_jiangli()) {
//                             AL_GAME_HUA_WelfareData.WelfareData.getInstance().jieshou_meiri_fenxiang_jiangli();
//                             var e =
//                                 AL_GAME_HUA_WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(3).rewardType;
//                             AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_jieshou_mianban({
//                                 rewards: e
//                             });
//                             AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportOutput({
//                                 source: AL_GAME_HUA_EventConfig.AssetsSource.welfare,
//                                 action: AL_GAME_HUA_EventConfig.AssetsAction.share_get,
//                                 goodsList: e.map(function (e) {
//                                     return {
//                                         goods: e.goodsId,
//                                         num: e.num
//                                     };
//                                 })
//                             });
//                         }
//                     }
//                 });
//                 break;
//             case "subscribe":
//                 if (!AL_GAME_HUA_WelfareData.WelfareData.getInstance().get_Have_Subscribe_Reward()) {
//                     return;
//                 }
//                 AL_APP_SDK_MANAGER.showLoading();
//                 var n = 0,
//                     o = 0,
//                     r = function () {
//                         if (n + o >= 1) {
//                             AL_APP_SDK_MANAGER.hideLoading();
//                             if (o > 0) {
//                                 AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_dingyue_mianban();
//                             } else {
//                                 {
//                                     AL_GAME_HUA_WelfareData.WelfareData.getInstance().jieshou_dingyue_jiangli();
//                                     var e =
//                                         AL_GAME_HUA_WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(
//                                             5
//                                         ).rewardType;
//                                     AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_jieshou_mianban({
//                                         rewards: e
//                                     });
//                                     AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportOutput({
//                                         source: AL_GAME_HUA_EventConfig.AssetsSource.welfare,
//                                         action: AL_GAME_HUA_EventConfig.AssetsAction.share_get,
//                                         goodsList: e.map(function (e) {
//                                             return {
//                                                 goods: e.goodsId,
//                                                 num: e.num
//                                             };
//                                         })
//                                     });
//                                 }
//                             }
//                         }
//                     };
//                 AL_GAME_HUA_PlatformManager.PlatformManager.getInstance().wx.requestSubscribeSystemMessage({
//                     success: function () {
//                         n++;
//                         r();
//                     },
//                     fail: function () {
//                         o++;
//                         r();
//                     }
//                 });
//         }
//     };
//     __decorate([b(cc.Node)], t.prototype, "layout", void 0);
//     __decorate([b(AL_GAME_HUA_GameClubButton.default)], t.prototype, "com_game_club_button", void 0);
//     return __decorate([m], t);
// })(AL_GAME_HUA_PopupSelf.default);
// exports.default = D;


// 引入模块
const i18nMgr = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_i18nMgr");
const AddPrefab = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_AddPrefab");
const Common = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Common");
const Define = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Define");
const PopupSelf = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_PopupSelf");
const GameClubButton = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_GameClubButton");
const PlatformManager = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_PlatformManager");
const EventConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_EventConfig");
const WelfareConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_WelfareConfig");
const WelfareData = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_WelfareData");

// Cocos Creator 装饰器
const { ccclass, property } = cc._decorator;

/**
 * 福利界面类
 */
@ccclass
class WelfareUI extends PopupSelf.default {
    @property(cc.Node)
    layout = null;

    @property(GameClubButton.default)
    com_game_club_button = null;

    inviteRed = null;
    shareRed = null;
    subscribeRed = null;
    gameClubRed = null;

    inviteCompleted = null;
    shareCompleted = null;
    subscribeCompleted = null;
    gameClubCompleted = null;

    start() {
        WelfareData.WelfareData.getInstance().chaxunYaoqingShuju();
        this.chushihua_ui();
    }

    onDestroy() {
        AL_APP_SDK_MANAGER.off(this);
    }

    chuli_xianshi_hou() {
        this.com_game_club_button.node.active = true;
    }

    /**
     * 初始化UI
     */
    chushihua_ui() {
        const subscribeNode = this.layout.children[3];
        this.subscribeRed = subscribeNode.getChildByName("red");

        this.chushihua_yaoqing();
        this.chushihua_fenxiang();
        this.chushihua_yixi_quan_gongneng();
        this.chushihua_dingyue_gongneng();
    }

    /**
     * 初始化邀请功能
     */
    chushihua_yaoqing() {
        const inviteNode = this.layout.children[0];
        this.inviteRed = inviteNode.getChildByName("red");

        const rewardBar = cc.find("rewardBar", inviteNode);
        this.inviteCompleted = cc.find("completed", rewardBar);

        const topLabel = cc.find("topLabel", rewardBar).getComponent(cc.Label);
        const rewardType0 = WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(0).rewardType;
        const rewardType1 = WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(1).rewardType;
        const rewardType2 = WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(2).rewardType;
        const totalRewards = rewardType0[0].num + rewardType1[0].num + rewardType2[0].num;

        topLabel.string = "x" + totalRewards;

        const updateInviteStatus = () => {
            const haveInviteReward = WelfareData.WelfareData.getInstance().huoqu_shifou_yongyou_yaoqing_jiangli();
            this.inviteRed.active = haveInviteReward;
            
            const allInviteReceived = WelfareData.WelfareData.getInstance().huoqu_suoyou_yaoqing_jiangli();
            this.inviteCompleted.active = allInviteReceived;
        };

        updateInviteStatus();

        AL_APP_SDK_MANAGER.on(Define.GameEvent.REFRESH_INVITE_LIST, updateInviteStatus, this);
    }

    /**
     * 初始化分享功能
     */
    chushihua_fenxiang() {
        const node_share = this.layout.children[1];
        this.shareRed = node_share.getChildByName("red");

        const rewardBar = cc.find("rewardBar", node_share);
        this.shareCompleted = cc.find("completed", rewardBar);

        const topLabel = cc.find("topLabel", rewardBar).getComponent(cc.Label);
        const rewardType = WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(3).rewardType;

        topLabel.string = "x" + rewardType[0].num.toString();

        const updateShareStatus = () => {
            const haveShareReward = WelfareData.WelfareData.getInstance().huoqu_shifou_yongyou_fenxiang_jiangli();
            this.shareRed.active = haveShareReward;
            this.shareCompleted.active = !haveShareReward;
        };

        updateShareStatus();

        AL_APP_SDK_MANAGER.on(Define.GameEvent.ON_DAILY_SHARE_REFRESH, updateShareStatus, this);
    }

    /**
     * 初始化游戏圈功能
     */
    chushihua_yixi_quan_gongneng() {
        const t = WelfareData.WelfareData.getInstance().huoqu_youxiquan_jieshou_jishu();
        const gameClubNode = this.layout.children[2];
        this.gameClubRed = gameClubNode.getChildByName("red");

        const rewardBar = cc.find("rewardBar", gameClubNode);
        this.gameClubCompleted = cc.find("completed", rewardBar);

        const topLabel = cc.find("topLabel", rewardBar).getComponent(cc.Label);
        const rewardType = WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(t === 0 ? 6 : 4).rewardType;

        topLabel.string = "x" + rewardType[0].num.toString();

        const titleLabel = cc.find("title", gameClubNode).getComponent(cc.Label);
        const descLabel = cc.find("desc", gameClubNode).getComponent(cc.Label);

        titleLabel.string = i18nMgr.i18nMgr._huoqu_biaoqian(t === 0 ? "加入游戏圈" : "游戏圈互动");
        descLabel.string = i18nMgr.i18nMgr._huoqu_biaoqian(t === 0 ? "加入游戏圈描述" : "游戏圈互动描述");

        const updateGameClubStatus = () => {
            const haveGameClubReward = WelfareData.WelfareData.getInstance().huoqu_shifou_yongyou_youxiquan_jiangli();
            this.gameClubRed.active = haveGameClubReward;
            this.gameClubCompleted.active = !haveGameClubReward;
        };

        updateGameClubStatus();

        AL_APP_SDK_MANAGER.on(Define.GameEvent.ON_GAME_CLUB_REFRESH, updateGameClubStatus, this);

        this.com_game_club_button.tianjia_anniu_dianji_huidiao(() => {
            if (WelfareData.WelfareData.getInstance().huoqu_shifou_yongyou_youxiquan_jiangli()) {
                WelfareData.WelfareData.getInstance().jieshou_youxiquan_jiangli();
                AddPrefab.AddPrefab.chuangjian_jieshou_mianban({ rewards: rewardType });
                EventConfig.EventConfig.getInstance().assetsReportOutput({
                    source: EventConfig.AssetsSource.welfare_club,
                    action: EventConfig.AssetsAction.get,
                    goodsList: rewardType.map(e => ({ goods: e.goodsId, num: e.num }))
                });
            }
        });
    }

    /**
     * 初始化订阅功能
     */
    chushihua_dingyue_gongneng() {
        const subscribeNode = this.layout.children[3];
        this.subscribeRed = subscribeNode.getChildByName("red");

        const rewardBar = cc.find("rewardBar", subscribeNode);
        this.subscribeCompleted = cc.find("completed", rewardBar);

        const topLabel = cc.find("topLabel", rewardBar).getComponent(cc.Label);
        const rewardType = WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(5).rewardType;

        topLabel.string = "x" + rewardType[0].num.toString();

        const updateSubscribeStatus = () => {
            const haveSubscribeReward = WelfareData.WelfareData.getInstance().get_Have_Subscribe_Reward();
            this.subscribeRed.active = haveSubscribeReward;
            this.subscribeCompleted.active = !haveSubscribeReward;
        };

        updateSubscribeStatus();

        AL_APP_SDK_MANAGER.on(Define.GameEvent.ON_SUBSCRIBE_REFRESH, updateSubscribeStatus, this);
    }

    /**
     * 处理按钮回调
     * @param {cc.Event} event - 事件对象
     * @param {string} action - 动作类型
     */
    handle_button_callback(event, action) {
        switch (action) {
            case "close":
                this.close();
                break;
            case "invite":
                this.close();
                AddPrefab.AddPrefab.chuangjian_yaoqing_mianban();
                break;
            case "share":
                Common.Common.share({
                    location: Define.Define.SHARE_LOCATION.LOGIN,
                    type: 3,
                    success: () => {
                        if (WelfareData.WelfareData.getInstance().huoqu_shifou_yongyou_fenxiang_jiangli()) {
                            WelfareData.WelfareData.getInstance().jieshou_meiri_fenxiang_jiangli();
                            const rewardType = WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(3).rewardType;
                            AddPrefab.AddPrefab.chuangjian_jieshou_mianban({ rewards: rewardType });
                            EventConfig.EventConfig.getInstance().assetsReportOutput({
                                source: EventConfig.AssetsSource.welfare,
                                action: EventConfig.AssetsAction.share_get,
                                goodsList: rewardType.map(e => ({ goods: e.goodsId, num: e.num }))
                            });
                        }
                    }
                });
                break;
            case "subscribe":
                if (!WelfareData.WelfareData.getInstance().get_Have_Subscribe_Reward()) {
                    return;
                }
                AL_APP_SDK_MANAGER.showLoading();
                let successCount = 0, failCount = 0;
                const handleSubscribeResult = () => {
                    if (successCount + failCount >= 1) {
                        AL_APP_SDK_MANAGER.hideLoading();
                        if (failCount > 0) {
                            AddPrefab.AddPrefab.chuangjian_dingyue_mianban();
                        } else {
                            WelfareData.WelfareData.getInstance().jieshou_dingyue_jiangli();
                            const rewardType = WelfareConfig.WelfareConfig.getInstance().huoqu_fuli_peizhi_genju_id(5).rewardType;
                            AddPrefab.AddPrefab.chuangjian_jieshou_mianban({ rewards: rewardType });
                            EventConfig.EventConfig.getInstance().assetsReportOutput({
                                source: EventConfig.AssetsSource.welfare,
                                action: EventConfig.AssetsAction.share_get,
                                goodsList: rewardType.map(e => ({ goods: e.goodsId, num: e.num }))
                            });
                        }
                    }
                };
                PlatformManager.PlatformManager.getInstance().wx.requestSubscribeSystemMessage({
                    success: () => {
                        successCount++;
                        handleSubscribeResult();
                    },
                    fail: () => {
                        failCount++;
                        handleSubscribeResult();
                    }
                });
                break;
        }
    }
}

exports.default = WelfareUI;
